<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8">
      <el-col :span="24">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sysJoinMain:sysJoinMain:add']"
          v-if="!sysJoinMainData"
        >新增加入我们信息</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['sysJoinMain:sysJoinMain:edit']"
          v-if="sysJoinMainData"
        >修改信息</el-button>
      </el-col>
    </el-row>

    <div v-if="!sysJoinMainData" class="empty-container">
      <el-empty description="暂无加入我们信息">
        <el-button type="primary" @click="handleAdd">添加加入我们信息</el-button>
      </el-empty>
    </div>

    <el-card v-else class="join-main-card">
      <div slot="header" class="clearfix">
        <span class="join-title">{{ sysJoinMainData.mainTitleZhCn }}</span>
        <div class="join-actions">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate"
            v-hasPermi="['sysJoinMain:sysJoinMain:edit']"
          >编辑</el-button>
        </div>
      </div>

      <div class="join-details">
        <!-- 中文信息 -->
        <div class="info-section">
          <h3><i class="el-icon-chat-line-round"></i> 中文信息</h3>
          <div class="info-item">
            <label>主标题:</label>
            <span>{{ sysJoinMainData.mainTitleZhCn }}</span>
          </div>
          <div class="info-item">
            <label>副标题:</label>
            <span>{{ sysJoinMainData.subTitleZhCn }}</span>
          </div>
        </div>

        <!-- 英文信息 -->
        <div class="info-section">
          <h3><i class="el-icon-chat-line-round"></i> 英文信息</h3>
          <div class="info-item">
            <label>主标题:</label>
            <span>{{ sysJoinMainData.mainTitleEnUs }}</span>
          </div>
          <div class="info-item">
            <label>副标题:</label>
            <span>{{ sysJoinMainData.subTitleEnUs }}</span>
          </div>
        </div>

        <!-- 德文信息 -->
        <div class="info-section">
          <h3><i class="el-icon-chat-line-round"></i> 德文信息</h3>
          <div class="info-item">
            <label>主标题:</label>
            <span>{{ sysJoinMainData.mainTitleDeDe }}</span>
          </div>
          <div class="info-item">
            <label>副标题:</label>
            <span>{{ sysJoinMainData.subTitleDeDe }}</span>
          </div>
        </div>

        <!-- 图片展示 -->
        <div class="info-section">
          <h3><i class="el-icon-picture-outline"></i> 图片展示</h3>
          <div class="image-preview-container">
            <image-preview :src="sysJoinMainData.imagePath" width="40%" height="auto"/>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 添加或修改加入我们信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="中文主标题" prop="mainTitleZhCn">
              <el-input v-model="form.mainTitleZhCn" placeholder="请输入中文主标题" />
            </el-form-item>
            <el-form-item label="中文副标题" prop="subTitleZhCn">
              <el-input v-model="form.subTitleZhCn" placeholder="请输入中文副标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文主标题" prop="mainTitleEnUs">
              <el-input v-model="form.mainTitleEnUs" placeholder="请输入英文主标题" />
            </el-form-item>
            <el-form-item label="英文副标题" prop="subTitleEnUs">
              <el-input v-model="form.subTitleEnUs" placeholder="请输入英文副标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="德文主标题" prop="mainTitleDeDe">
              <el-input v-model="form.mainTitleDeDe" placeholder="请输入德文主标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="德文副标题" prop="subTitleDeDe">
              <el-input v-model="form.subTitleDeDe" placeholder="请输入德文副标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="图片" prop="imagePath">
              <image-upload v-model="form.imagePath"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysJoinMain, getSysJoinMain, delSysJoinMain, addSysJoinMain, updateSysJoinMain } from "@/api/sysJoinMain/sysJoinMain"

export default {
  name: "SysJoinMain",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 加入我们页面配置数据
      sysJoinMainData: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mainTitleZhCn: [
          { required: true, message: "中文主标题不能为空", trigger: "blur" }
        ],
        mainTitleEnUs: [
          { required: true, message: "英文主标题不能为空", trigger: "blur" }
        ],
        mainTitleDeDe: [
          { required: true, message: "德文主标题不能为空", trigger: "blur" }
        ],
        subTitleZhCn: [
          { required: true, message: "中文副标题不能为空", trigger: "blur" }
        ],
        subTitleEnUs: [
          { required: true, message: "英文副标题不能为空", trigger: "blur" }
        ],
        subTitleDeDe: [
          { required: true, message: "德文副标题不能为空", trigger: "blur" }
        ],
        imagePath: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询加入我们页面配置 */
    getList() {
      this.loading = true
      listSysJoinMain(this.queryParams).then(response => {
        if (response.rows && response.rows.length > 0) {
          this.sysJoinMainData = response.rows[0]
        } else {
          this.sysJoinMainData = null
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        joinId: null,
        mainTitleZhCn: null,
        mainTitleEnUs: null,
        mainTitleDeDe: null,
        subTitleZhCn: null,
        subTitleEnUs: null,
        subTitleDeDe: null,
        imagePath: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加加入我们信息"
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset()
      getSysJoinMain(this.sysJoinMainData.joinId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改加入我们信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.joinId != null) {
            updateSysJoinMain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSysJoinMain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete() {
      const joinId = this.sysJoinMainData.joinId
      this.$modal.confirm('是否确认删除该加入我们信息？').then(() => {
        return delSysJoinMain(joinId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.join-main-card {
  margin-top: 20px;
}

.join-title {
  font-size: 18px;
  font-weight: bold;
}

.join-actions {
  float: right;
}

.join-details {
  display: grid;
  gap: 20px;
}

.info-section {
  padding: 15px;
  border-radius: 4px;
  background-color: #f9f9f9;
}

.info-section h3 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #409EFF;
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
}

.info-item {
  margin-bottom: 10px;
  display: flex;
}

.info-item label {
  font-weight: bold;
  min-width: 80px;
  color: #666;
}

.info-item span {
  flex: 1;
}

.image-preview-container {
  margin-top: 15px;
  border: 1px solid #eee;
  border-radius: 4px;
  overflow: hidden;
}

.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
}
</style>
